package com.budwk.app.sqfw.services.impl;

import com.alibaba.fastjson.JSON;
import com.budwk.app.sqfw.component.CommunityStreetComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.enums.RectificationStatusEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.services.SqfwFormSummaryService;
import com.budwk.app.sqfw.services.WasteClassificationService;
import com.budwk.app.sqfw.vo.AreaAddressVo;
import com.budwk.app.sqfw.vo.Sqfw_formSummaryForAdminVO;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.AuthorityUtil;
import com.budwk.app.util.StrUtil;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SimpleCriteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.ArrayList;
import java.util.List;

@IocBean
@Slf4j
public class WasteClassificationServiceImpl implements WasteClassificationService {

    @Inject
    private SqfwFormSummaryService sqfwFormSummaryService;
    @Inject
    private SysUserService sysUserService;
    @Inject
    private CommunityStreetComponent communityStreetComponent;

    @Override
    public Result<?> listWasteClassification(Sqfw_formSummaryForAdminVO forAdmin) {
        // 1 构建查询条件
        SimpleCriteria criteria = buildQueryCond(forAdmin);
        // 2 分页查询
        Pagination pagination = sqfwFormSummaryService.listPage(forAdmin.getPageNo(), forAdmin.getPageSize(), criteria);
        if (pagination.getTotalCount() > 0) {
            // 空值处理
            List<Sqfw_formSummary> results = new ArrayList<>(50);
            for (Sqfw_formSummary formSummary : pagination.getList(Sqfw_formSummary.class)) {
                results.add(StrUtil.formatSummaryNullStr(formSummary));
            }
            pagination.setList(results);
        }
        // 2 分页查询
        return Result.data(pagination);
    }

    @Override
    public SimpleCriteria buildQueryCond(Sqfw_formSummaryForAdminVO forAdmin) {
        // 1 构建查询参数
        SimpleCriteria cri = Cnd.cri();
        SqlExpressionGroup expressionSql = cri.where();
        expressionSql.andNotEquals(Sqfw_formSummary::getCreateDate, CommonConstants.WASTE_CREATE_TIME);
        expressionSql.andEquals(Sqfw_formSummaryForAdminVO::getType, SqfwFormSummaryConstants.refuse_classify);

        if (Strings.isNotBlank(forAdmin.getPageOrderName()) && Strings.isNotBlank(forAdmin.getPageOrderBy())) {
            if (forAdmin.getPageOrderBy().equals(CommonConstants.ASC)) {
                cri.getOrderBy().asc(forAdmin.getPageOrderName());
            }
            if (forAdmin.getPageOrderBy().equals(CommonConstants.DESC)) {
                cri.getOrderBy().desc(forAdmin.getPageOrderName());
            }
        } else {
            cri.getOrderBy().desc(Sqfw_formSummary::getFillDate);
        }
        AreaAddressVo areaAddressVo = AuthorityUtil.checkFormSummaryAreaPermission(forAdmin);
        AuthorityUtil.commonPermissionLikeQuery(expressionSql, areaAddressVo);
        // 调查地点 a3；
        if (StringUtils.isNotEmpty(forAdmin.getA3())) {
            expressionSql.andLike(Sqfw_formSummary::getA3, forAdmin.getA3());
        }
        // ○排查问题 a20
        if (StringUtils.isNotEmpty(forAdmin.getA20())) {
            expressionSql.andLike(Sqfw_formSummary::getA20, forAdmin.getA20());
        }
        // ○整改状态a17；
        if (StringUtils.isNotEmpty(forAdmin.getA17())) {
            if (!forAdmin.getA17().equals(RectificationStatusEnum.ALL.getName())) {
                expressionSql.andLike(Sqfw_formSummary::getA17, forAdmin.getA17());
            }
        }
        // ○整改措施a18;
        if (StringUtils.isNotEmpty(forAdmin.getA18())) {
            expressionSql.andLike(Sqfw_formSummary::getA18, forAdmin.getA18());
        }
        // ○检查人员 crateUserName;
        if (StringUtils.isNotEmpty(forAdmin.getCreateUserName())) {
            expressionSql.andLike(Sqfw_formSummary::getCreateUserName, forAdmin.getCreateUserName());
        }
        // 巡查时间fiilDate
        CommunityStreetComponent.handleFillQueryTime(forAdmin, expressionSql);
        // 全局搜索
        if (Strings.isNotBlank(forAdmin.getQueryAll())) {
            String queryAll = forAdmin.getQueryAll();
            SqlExpressionGroup community = Cnd.exps(Sqfw_formSummaryForAdminVO::getCommunity, "like", String.format("%s%s%s", "%", queryAll, "%"));
            SqlExpressionGroup a3 = Cnd.exps(Sqfw_formSummaryForAdminVO::getA3, "like", String.format("%s%s%s", "%", queryAll, "%"));
            SqlExpressionGroup a20 = Cnd.exps(Sqfw_formSummaryForAdminVO::getA20, "like", String.format("%s%s%s", "%", queryAll, "%"));
            SqlExpressionGroup a17 = Cnd.exps(Sqfw_formSummaryForAdminVO::getA17, "like", String.format("%s%s%s", "%", queryAll, "%"));
            SqlExpressionGroup a18 = Cnd.exps(Sqfw_formSummaryForAdminVO::getA18, "like", String.format("%s%s%s", "%", queryAll, "%"));
            SqlExpressionGroup createUserName = Cnd.exps(Sqfw_formSummaryForAdminVO::getCreateUserName, "like", String.format("%s%s%s", "%", queryAll, "%"));
            expressionSql.and(a3.or(a20).or(a17).or(a18).or(community).or(createUserName));
        }
        return cri;
    }

    @Override
    public String queryExportDataPDF(Sqfw_formSummaryForAdminVO forAdminVo) {
        // 1 构建查询条件
        SimpleCriteria cnd = buildQueryCond(forAdminVo);
        // 按照条件导出PDF
        String url = communityStreetComponent.generateServiceRecordPdfCollZipForBM(cnd);
        log.info("waste queryExportDataPDF info result: {}", url);
        return url;
    }

    @Override
    public String queryExportDataExcel(Sqfw_formSummaryForAdminVO bean) {
        SimpleCriteria cnd = buildQueryCond(bean);
        String url = communityStreetComponent.generateServiceRecordExcelCollZipForBM(bean, cnd);
        log.info("waste queryExportDataExcel info result: {}", url);
        return url;
    }
}
